// task_client_node.hpp
#ifndef TASK_CLIENT_NODE_HPP_
#define TASK_CLIENT_NODE_HPP_

#include <rclcpp/rclcpp.hpp>
#include <rclcpp_action/rclcpp_action.hpp>
#include <rms_pkg/action/task_execution.hpp>
#include <QVariant>
#include <QString>
#include <QObject>
#include "../include/global_logger.h"
#include <memory>

using TaskExecution = rms_pkg::action::TaskExecution;
using GoalHandle = rclcpp_action::ClientGoalHandle<TaskExecution>;

class TaskClientNode : public QObject, public rclcpp::Node
{
    Q_OBJECT

public:
    explicit TaskClientNode(const std::string& namespace_name = "");
    ~TaskClientNode();

    // 发送任务
    Q_INVOKABLE void sendTask(const QString& task_id,
        const QString& task_type,
        const QString& action,
        const QString& task_start_location = "",
        const QString& task_end_location = "",
        const QString& lobe_data = "",
        int priority = 5,
        const QString& json_data = "",
        int cmd_id = 0);
    
    // 设置命名空间并重新创建客户端
    Q_INVOKABLE void setNamespace(const QString& namespace_name);
    
    // 清理资源的方法
    Q_INVOKABLE void cleanup();

signals:
    void feedbackReceived(const QString& task_id, int progress, const QString& status, const QString& subtask);
    void resultReceived(const QString& task_id, const QString& status, const QString& message);

private:
    // 日志记录器
    std::shared_ptr<spdlog::logger> logger_;
    // 任务执行客户端
    rclcpp_action::Client<TaskExecution>::SharedPtr action_client_;
    // 当前命名空间
    std::string namespace_name_;
    // 客户端是否已创建
    bool client_created_;
    // 任务执行回调
    void goalResponseCallback(GoalHandle::SharedPtr goal_handle);
    // 任务执行反馈
    void feedbackCallback(GoalHandle::SharedPtr, const std::shared_ptr<const TaskExecution::Feedback> feedback);
    // 任务执行结果
    void resultCallback(const GoalHandle::WrappedResult& result);
    // 创建或重新创建Action客户端
    void createActionClient();
};

#endif  // TASK_CLIENT_NODE_HPP_